package com.gitee.feizns.quickstart.web.ex.impl;

import com.gitee.feizns.quickstart.domain.vo.Ret;
import com.gitee.feizns.quickstart.web.ex.ErrorCode;
import com.gitee.feizns.quickstart.web.ex.ServiceException;
import org.springframework.http.ResponseEntity;

/**
 * 业务异常解析处理。
 * @author feizns
 * @since 2019/7/7
 */
public class ServiceExceptionHandler extends AbstractExceptionHandler<ServiceException> {

    /**
     * 先从已注册的业务码中查找
     * @param e 异常实例
     * @return {@link ResponseEntity}
     */
    @Override
    public ResponseEntity<Ret<?>> handle(ServiceException e) {
        //响应在异常总配置的异常码
        ErrorCode code = e.getErrorCode().orElse(null);
        if ( code != null )
            return ResponseEntity.ok(Ret.error(e.getCode(), e.getMsg()));
        //默认的异常错误
        return ResponseEntity.ok(Ret.error(e.getMsg()));
    }

}
